package com.leyou.auth.controller;

import com.leyou.auth.service.AuthService;
import com.leyou.common.auth.domain.UserInfo;
import com.leyou.user.client.UserClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@RestController
@RequestMapping
public class authController {
    @Autowired
    private AuthService authService;

    /**
     *  用户登录
     */

    @PostMapping("/login")
    public ResponseEntity<Void> login(
            @RequestParam("username") String username,
            @RequestParam("password") String password,
            HttpServletResponse response){
        authService.login(username,password,response);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();

    }


    /**
     * 在页面上显示当前认证通过的用户名提供处理器
      */

        @GetMapping("/verify")
        public ResponseEntity<UserInfo>verifyUser(HttpServletRequest request,HttpServletResponse response){
        UserInfo userInfo=authService.verifyUser(request,response);
        return ResponseEntity.ok(userInfo);
        }

    /**
     * 退出登录
     */
    @PostMapping("/logout")
    public ResponseEntity<Void>logout(HttpServletRequest request,HttpServletResponse response){
        authService.logout(request,response);
        return  ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }





}
